package com.yuji.aacar.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yuji.aacar.R;
import com.yuji.aacar.controller.login.LoginActivity;
import com.yuji.aacar.controller.message.MessageActivity;
import com.yuji.aacar.controller.splash.SplashActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.core.mvvm.appbase.AppRoute;
import com.yuji.aacar.core.mvvm.appbase.CoreBaseActivity;
import com.yuji.aacar.tools.StatusBarUtils;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.dialogue.MyAlertDialogue;

import static com.yuji.aacar.R.id.back_image;

/**
 * <li>Package:com.yuji.aacar.base</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/2/22</li>
 * <li>Description:   </li>
 */

public class BaseActivity extends CoreBaseActivity {


    public View.OnClickListener backClickListener;
    public View.OnClickListener messageClickListener;
    public LayoutInflater mLayoutInflater;

    private ImageView messageImage; //消息
    private AutoRelativeLayout titleMessage; //消息

    private MyAlertDialogue mDialog;
    public boolean isFullScreen = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isFullScreen){
            setTitleBarColor(R.color.titleBarColor);
        }
        mLayoutInflater = LayoutInflater.from(this);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_DUM_NORMAL);
    }


    public void initTitle(boolean isNeedBackImage, String titleContentStr, String titleRightContentStr) {

        AutoRelativeLayout backLayout = (AutoRelativeLayout) findViewById(back_image);
        ImageView backImage = (ImageView) findViewById(R.id.title_back);
        TextView titleContent = (TextView) findViewById(R.id.title_content);
        TextView titleRightContent = (TextView) findViewById(R.id.back_login);
        titleMessage = (AutoRelativeLayout) findViewById(R.id.title_message_rl);
        messageImage = (ImageView) findViewById(R.id.title_message_iv);

        if (isNeedBackImage) {
            backLayout.setVisibility(View.VISIBLE);

            if (null == backClickListener){
                backLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                backImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Tools.closeKeyBroad(mContext, v);
                        finish();
                    }
                });

            } else {
                backLayout.setOnClickListener(backClickListener);
                backImage.setOnClickListener(backClickListener);
            }

        } else {
            backLayout.setVisibility(View.GONE);
        }


        if (null != titleContentStr && !TextUtils.isEmpty(titleContentStr)){
            titleContent.setVisibility(View.VISIBLE);
            titleContent.setText(titleContentStr);
        } else {
            titleContent.setVisibility(View.GONE);
        }

        if (null != titleRightContentStr && !TextUtils.isEmpty(titleRightContentStr)){
            titleRightContent.setVisibility(View.VISIBLE);
            titleMessage.setVisibility(View.GONE);
            titleContent.setText(titleContentStr);
            if (titleRightContentStr.contentEquals("登录")){
                titleRightContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CoreEventCenter.postMessage(EventConst.LOGIN,"登录");
                    }
                });
            }
        } else {
            titleRightContent.setVisibility(View.GONE);
        }
    }


    public void initTitle(boolean isNeedBackImage, String titleContentStr, String titleRightContentStr, boolean presentMessageActivity) {

        this.initTitle(isNeedBackImage, titleContentStr, titleRightContentStr);
        if (presentMessageActivity){
            titleMessage.setVisibility(View.VISIBLE);
            messageImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presentController(MessageActivity.class);
                }
            });
        }
    }


    public void changeMessageStatus (int status) {
        switch (status){
            case 0:
                messageImage.setBackground(getResources().getDrawable(R.drawable.selector_message_no));
                break;
            case 1:
                messageImage.setBackground(getResources().getDrawable(R.drawable.selector_message_yes));
                break;
        }
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        LogUtil.e("requestSuccess", response);
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
        LogUtil.e("requestFailed", response.getMessage());
        showToast(response.getMessage());
    }


    /***************
     * Activity跳转
     * @param activity
     */
    public void presentController(Class activity) {
        AppRoute.getInstance().nextController(this, activity, AppRoute.WITHOUT_RESULTCODE);
        // 第一个参数：进入的
        // 第二个参数：退出的
//                this.overridePendingTransition(R.anim.inside_translate, R.anim.outside_translate);
    }

    /***************
     * Activity跳转 有值传递， 不需要resultCode
     * @param activity
     * @param intent
     */
    public void presentController(Class activity, Intent intent) {
        AppRoute.getInstance().nextControllerWithIntent(this, activity, AppRoute.WITHOUT_RESULTCODE, intent);
        // 第一个参数：进入的
        // 第二个参数：退出的
//                this.overridePendingTransition(R.anim.inside_translate, R.anim.outside_translate);
    }

    /***************
     * Activity跳转 ， 需要resultCode
     * @param activity
     * @param resultCode
     */
    public void presentController(Class<? extends BaseActivity> activity, int resultCode) {
        AppRoute.getInstance().nextController(this, activity, resultCode);
        // 第一个参数：进入的
        // 第二个参数：退出的
        //                this.overridePendingTransition(R.anim.inside_translate, R.anim.outside_translate);
    }

    /***************
     * Activity跳转 ， 需要resultCode
     * @param activity
     * @param resultCode
     */
    public void presentController(Class<? extends BaseActivity> activity, Intent intent, int resultCode) {
        AppRoute.getInstance().nextControllerWithIntent(this, activity, resultCode, intent);
        // 第一个参数：进入的
        // 第二个参数：退出的
        //                this.overridePendingTransition(R.anim.inside_translate, R.anim.outside_translate);
    }

    public void setTitleBarColor (int colorId) {
        StatusBarUtils.with(this).setColor(getResources().getColor(colorId)).init();
    }


    public void showAlertDialogue (String title, String singleMessage, String confirmMessage) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(this);
        ibuilder.setTitle(title);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, null);
        mDialog = ibuilder.create();
        if (null != mDialog && !mDialog.isShowing()){
            mDialog.show();
        }

    }

    public void showAlertDialogue (String title, String singleMessage,DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(this);
        ibuilder.setTitle(title);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton("知道了", confirmListener);
        mDialog = ibuilder.create();
        if (null != mDialog && !mDialog.isShowing()){
            mDialog.show();
        }

    }

    public void showAlertDialogue (String cancelMessage, String singleMessage, String confirmMessage, DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(this);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, confirmListener);
        ibuilder.setNegativeButton(cancelMessage, null);
        mDialog = ibuilder.create();
        if (null != mDialog && !mDialog.isShowing()){
            mDialog.show();
        }
    }

    public void showAlertDialogue (String cancelMessage, String titleMessage, String singleMessage, String confirmMessage, DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(this);
        ibuilder.setTitle(titleMessage);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, confirmListener);
        ibuilder.setNegativeButton(cancelMessage, null);
        mDialog = ibuilder.create();
        if (null != mDialog && !mDialog.isShowing()){
            mDialog.show();
        }
    }



    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
        if (message.getMessageCode().contentEquals(String.valueOf(EventConst.LOGIN))){
            if (this instanceof LoginActivity){
            } else {
                this.finish();
            }
        } else if (message.getMessageCode().contentEquals(String.valueOf(EventConst.REGISTER))){
            this.finish();
        } else if(message.getMessageCode().contentEquals(String.valueOf(EventConst.RELOGIN))){
            presentController(SplashActivity.class);
            this.finish();
        } else if (message.getMessageCode().contentEquals(String.valueOf(EventConst.PAY_FINISH))){
            this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
